package com.ruoyi.lvlian.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.lvlian.domain.ProductCategory;
import com.ruoyi.lvlian.dto.ProductCategoryDto;
import com.ruoyi.lvlian.service.IProductCategoryService;
import com.ruoyi.lvlian.service.ISmsSendService;
import com.ruoyi.lvlian.vo.ProductCategoryFlatVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 产品分类Controller
 *
 * @author lvlian
 * @date 2025-04-15
 */
@Api(value = "一、基础参数-货品分类-元数据", tags = "一、基础参数-货品-元数据")
@RestController
@RequestMapping("/ProductCategory")
public class ProductCategoryController extends BaseController {
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private ISmsSendService smsSendService;
    /**
     * 查询产品分类列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductCategoryDto productCategory) {
        startPage();
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        return getDataTable(list);
    }

    @GetMapping("/queryFlat")
    public TableDataInfo queryFlat(ProductCategoryDto productCategory) {
        startPage();
        List<ProductCategoryFlatVo> list = productCategoryService.queryFlat(productCategory);
        return getDataTable(list);
    }

    @ApiOperation("获取产品分类树")
    @GetMapping("/tree")
    public AjaxResult getCategoryTree(ProductCategoryDto productCategory) {
        List<ProductCategory> categoryTree = productCategoryService.getCategoryTree(productCategory);
        return success(categoryTree);
    }

    /**
     * 获取产品分类详细信息
     */
    @ApiOperation("查询单个分类树")
    @GetMapping(value = "/getInfoByIdAndLevel/{id}/{level}")
    public AjaxResult getInfoByIdAndLevel(
            @PathVariable("id") @NotNull(message = "ID不能为空") String id,
            @PathVariable("level") @NotBlank(message = "级别不能为空") String level) {
        return success(productCategoryService.selectProductCategoryById(id, level));
    }

    /**
     * 新增产品分类
     */
    @RequiresPermissions("lvlian:ProductCategory:add")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategory productCategory) {
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    /**
     * 修改产品分类
     */
    @RequiresPermissions("lvlian:ProductCategory:edit")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated ProductCategory productCategory) {
        return toAjax(productCategoryService.updateProductCategory(productCategory));
    }

    /**
     * 删除产品分类
     */
//    @RequiresPermissions("lvlian:ProductCategory:remove")
//    @Log(title = "产品分类", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(productCategoryService.deleteProductCategoryByIds(ids));
//    }

    @RequiresPermissions("lvlian:ProductCategory:remove")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody ProductCategoryDto productCategory) {
        return toAjax(productCategoryService.deleteProductCategory(productCategory));
    }
}